package com.genesis.yunnanji.view;

import android.support.v4.app.Fragment;
import com.flyco.tablayout.SlidingTabLayout;
import com.genesis.yunnanji.Constants;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.ClassifySortAdapter;
import com.genesis.yunnanji.bean.BaseBean;
import com.genesis.yunnanji.bean.MessageBean;
import com.genesis.yunnanji.utils.NetUtils;
import com.genesis.yunnanji.view.fragment.AllyConsumptionFragment;
import com.genesis.yunnanji.view.fragment.AllyIncomeFragment;
import com.genesis.yunnanji.widget.NoScrollViewPager;
import com.youth.xframe.widget.XToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;


/**
 * 创建日期：2018/9/26 on 11:34
 * 作者:Kite
 * 描述:购物卡
 */
public class AllyActivity extends BaseActivity {

    @BindView(R.id.vp_content)
    NoScrollViewPager vpContent;

    @BindView(R.id.tabs)
    SlidingTabLayout tabs;

    private ArrayList<String> functionList = new ArrayList<>();//收入、消费按钮

    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_ally;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("购物卡");
        fragmentList.add(AllyIncomeFragment.newInstance());
        fragmentList.add(AllyConsumptionFragment.newInstance());

        functionList.add("收入");
        functionList.add("消费");
        ClassifySortAdapter classifySortAdapter = new ClassifySortAdapter(getSupportFragmentManager(), fragmentList, functionList);
        vpContent.setAdapter(classifySortAdapter);
        tabs.setViewPager(vpContent);
        vpContent.setCurrentItem(0);
        vpContent.setOffscreenPageLimit(2);
        getAllyInfo();
    }

    @OnClick(R.id.tv_binding)
    public void onViewClicked() {
        advance(BindingCardActivity.class);
    }

    private void getAllyInfo() {
        JSONObject params = new JSONObject();
        try {
            params.put("__apptoken__", Constants.USER_TOKEN);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = NetUtils.initNet(params);
        EasyHttp.post(Constants.CARD_INDEX)
//                .baseUrl(Constants.MAIN_ADMIN)
                .requestBody(requestBody)
                .execute(new CallBackProxy<BaseBean<MessageBean>, MessageBean>(new SimpleCallBack<MessageBean>() {
                    @Override
                    public void onError(ApiException e) {
                        XToast.error(e.getMessage());
                    }

                    @Override
                    public void onSuccess(MessageBean bean) {
//                        EventBus.getDefault().post(bean);
                    }
                }) {
                });
    }

}
